package com.fs.sw.controller;

import com.fs.sw.bo.UserBO;
import com.fs.sw.dao.UsersRepository;
import com.fs.sw.pojo.Roles;
import com.fs.sw.pojo.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/session")
@Slf4j
public class SessionHello {
	
	@Autowired
	private UsersRepository usersRepository;
	
    @GetMapping("/getUser")
    public Users getUser(HttpServletRequest request){
		Users u = usersRepository.testfind(2);
        Roles roles =u.getRoles();
        System.out.println(roles.getRolename());
		return u;
    }

    @GetMapping("/UserBO")
    public UserBO getUserBO(HttpServletRequest request){
        Users u = usersRepository.testfind(2);
        UserBO bo = new UserBO();
        BeanUtils.copyProperties(u, bo);
//        bo.setSex(true);
//        bo.setRemarks("你好");
        log.warn("aaaa");
        return bo;
    }
	
    /** 放入session进redis */
    @GetMapping("/setSession")
    public String setSession(HttpServletRequest request){
        request.getSession().setAttribute("LoginUser", "亲爱的EASTW用户");
        String requestURI = request.getRequestURI();
        return requestURI;
    }

    /** 获取session */
    @GetMapping("/getSession")
    public String getSession(HttpServletRequest request){
        String attribute = (String) request.getSession().getAttribute("LoginUser");
        return attribute + ":" + request.getSession().getId();
    }
}
